import {
  asyncRoutes,
  constantRoutes
} from '@/router'
import {getUserManues } from '@/api/user'
import Layout from '@/layout'
/**
 * Use meta.role to determine if the current user has permission
 * @param roles
 * @param route
 */
function hasPermission(roles, route) {
  if (route.meta && route.meta.roles) {
    return roles.some(role => route.meta.roles.includes(role))
  } else {
    return true
  }
}

export function generateRouter(menu){
  menu.forEach(item => {
    if(item.children){
      item.component = Layout;
      generateRouter(item.children);
    }else{
      item.component=() => Promise.resolve().then(()=>require(`@/views/${item.url}`))
      // if(`${item.menuId}`==2){
       
      //   let path='dashboard/index'
      //   let path2=item.url;

      //   console.log("path: "+path)
      //   console.log("path2: "+path2)

      //   //item.component=() => import(`@/views/${a}`)
      //   //item.component=() => import(`@/views/${item.url}`)
      //   item.component=() => Promise.resolve().then(()=>require(`@/views/${item.url}`))
      // }
      // if(`${item.menuId}`==7){
      //   console.log(777)
      //   item.component=() => import('@/views/form/index')
      // }
    }
  })
  // menu[0].component = Layout;
  // menu[0].children[0].component= () => import('@/views/dashboard/index')
  // menu[1].component = Layout;
  // menu[1].children[0].component= () => import('@/views/form/index')


  return menu;

}

export function setUpMenus(menu){
  const res = []
  menu.forEach(item => {
    const tmp = {
      ...item
    }
    if (tmp.children) {
      tmp.component = Layout;
      tmp.children = setUpMenus(tmp.children);
    } else {
      console.log("ssss: ",`${item.url}`)
      tmp.component = () => import(`${item.url}`)
    }
      res.push(tmp)
  })
  return res


  // menu.forEach(item => {
  //   if (item.children) {
  //     item.component = Layout;
  //     item.children = setUpMenus(item.children);
  //   } else {
  //     console.log("ssss: ",`${item.url}`)
  //     item.component = () => import(`${item.url}`)
  //   }
  // })
  //return menu
}

/**
 * Filter asynchronous routing tables by recursion
 * @param routes asyncRoutes
 * @param roles
 */
export function filterAsyncRoutes(routes, roles) {
  const res = []

  routes.forEach(route => {

    const tmp = {
      ...route
    }
    console.log("tmp: ", tmp)
    if (hasPermission(roles, tmp)) {
      if (tmp.children) {
        tmp.children = filterAsyncRoutes(tmp.children, roles)
      }
      res.push(tmp)
    }
  })

  return res
}

const state = {
  routes: [],
  addRoutes: []
}

const mutations = {
  SET_ROUTES: (state, routes) => {
    
    //state.addRoutes = routes
    state.routes = routes
  //  state.routes = constantRoutes.concat(routes)
    console.log("accessedRoutes111111-----",routes)
  }

}

const actions = {

  generateRoutes({
    commit
  }) {

    return new Promise((resolve, reject) => {
      getUserManues().then(response => {
        const { data } = response;
        console.log('data--- ',data)

        let asyncRouter=generateRouter(data);

        //JSON.stringify(asyncRouter)
        console.log("ggsss",asyncRouter)
      //  let b=setUpMenus(data)

   //     console.log('b:123132 ',b)

        // let accessedRoutes;
        // accessedRoutes = setUpMenus(data)

        // console.log('accessedRoutes:123132 ',accessedRoutes)

        commit('SET_ROUTES', [...asyncRouter])
       
        resolve([...asyncRouter])
      }).catch(error => {
        reject(error)
      })
    })


  },


  generateRoutesMock({ commit }, roles) {
    return new Promise(resolve => {
      let accessedRoutes
      // if (roles.includes('admin')) {
      //   accessedRoutes = asyncRoutes || []
      // } else {
      //   accessedRoutes = filterAsyncRoutes(asyncRoutes, roles)
      // }

      accessedRoutes = asyncRoutes 
      commit('SET_ROUTES', accessedRoutes)
      resolve(accessedRoutes)
    })
  }



}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
